package com.ruoyi.framework.dataScope.strategy;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysRole;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Classname ThisLevelChildenDataScope
 * @Description 本级以及子级
 * @Author Created by Lihaodong (alias:小东啊) lihaodongmail@163.com
 * @Date 2019-06-08 16:30
 * @Version 1.0
 */
@Component("4")
public class ThisLevelChildenDataAuth implements AbstractDataAuthHandler {

    @Autowired
    private TokenService tokenService;

    @Autowired
    @Qualifier("masterJdbcTemplate")
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<Long> getDeptIds(SysRole role, String scopeType) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        String sql="select dept_id as deptId from sys_dept where find_in_set( ?, ancestors )";
        //List<SysDept> list = deptService.list(new QueryWrapper<SysDept>().lambda().apply("find_in_set( {0} , ancestors )", user.getDeptId()));
        //List<Long> deptIds= list.stream().map(s -> s.getDeptId()).collect(Collectors.toList());
        List<Long> longs = jdbcTemplate.queryForList(sql, Long.class, user.getDeptId());
        return longs;
    }
}
